package org.dxw.common.listener;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.dxw.DO.UserDO;
import org.dxw.common.event.RechargeMoneyEvent;
import org.dxw.common.event.UserRegisterEvent;
import org.dxw.outApi.EmailService;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @Author dxw
 * @Date 2023/11/18 23:36
 * @Description
 */
@Component
@Slf4j
public class EmailListener  {

    @Autowired
    private EmailService emailService;

    @EventListener
    @Order(1)
    @Async
    public void sendEmail(RechargeMoneyEvent event) {
        UserDO userDO = event.getUserDO();
        log.info("事件发布来源："+ JSON.toJSONString(event));
        String ordreNo = event.getOrdreNo();
        String content = "充值成功,订单号："+ordreNo;
        emailService.sendMail(userDO.getEmail(), content);
    }

    @EventListener
    @Order(1)
    @Async
    public void sendEmail(UserRegisterEvent event) {
        UserDO userDO = event.getUserDO();
        log.info("事件发布来源："+ JSON.toJSONString(event));
        String content = "注册成功,用户账号："+userDO.getUserName()+"，用户名："+userDO.getNickName();
        emailService.sendMail(userDO.getEmail(), content);
    }


}
